<template>
  <div>
    <div id="paper">
      <h1>{{wiki_title}}</h1>
      <h5>
      <span>
        <!--<i class="iconfont iconyanjing"></i>-->
        &nbsp;浏览次数&nbsp;{{wiki_read_num}}
      </span>
        <span style="display: none" v-show="isLogin">
            <i class="iconfont iconbianji" @click="editePage(wiki_id)"></i>
          </span>
        <span style="display: none" v-show="isLogin">
            <i class="iconfont iconpicture-delet" @click="delPage(wiki_id)"></i>
          </span>
        <span>
           &nbsp;最后编辑于&nbsp;{{wiki_last_time}}
      </span>
      </h5>
      <CuText :text="wiki_text"></CuText>
    </div>
    <Rside class="rside-pager"></Rside>
  </div>
</template>

<script>
import showdown from 'showdown'
import hljs from 'highlight.js'
export default {
  name: 'paper',
  data () {
    return {
      wiki_id: null,
      wiki_text: null,
      wiki_title: null,
      wiki_read_num: null,
      wiki_last_time: null,
      isLogin: sessionStorage.userName
    }
  },
  created () {
    this.get_wiki()
  },
  updated () {
    this.Hljs()
  },
  methods: {
    Hljs () {
      document.querySelectorAll('pre code').forEach((block) => {
        hljs.highlightBlock(block)
      })
    },
    get_wiki () {
      let wikiId = this.$route.params.id
      this.wiki_id = wikiId
      let converter = new showdown.Converter()
      this.$http.get(process.env.REST_URL + 'wiki/getWiki/' + wikiId).then((res) => {
        // console.log(res)
        let block = converter.makeHtml(res.data.data.content)
        // this.wiki_text = Hljs.highlightBlock(block)
        this.wiki_text = block
        this.wiki_title = res.data.data.title
        this.wiki_read_num = res.data.data.read_num
        this.wiki_last_time = res.data.data.last_time
        document.title = res.data.data.title
      }, function (response) {
        console.log('error')
      })
    },
    editePage (wikiId) {
      console.log('run editorPage')
      this.$router.push({name: 'editor', query: {id: wikiId}})
    },
    delPage (wikiId) {
      console.log('run delPage')
      this.$http.get(process.env.REST_URL + 'wiki/delWiki/' + wikiId).then((res) => {
        // console.log(res)
        if (res.data.status === 0) {
          alert('已删除一条wiki !')
        }
      }, function (response) {
        console.log('error')
      })
    }
  }

}
</script>
<style scoped>
  #paper {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    /*margin: 60px auto auto auto;*/
    /*max-width: 75%;*/
    display:inline-block;
    vertical-align:top;
    width: 64%;
    margin-right: 43px;
    box-shadow: 0 8px 17px 0 rgba(0,0,0,.12), 0 6px 20px 0 rgba(0,0,0,.14);
  }
  h5{
    color: #999;
  }
  .rside-pager{
    display:inline-block;
    background: #fafafa;
    vertical-align:top;
    width: 25%;
  }
</style>
